/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import controlador.Conecion;
import controlador.Reporte;
import controlador.Utilidades;
import controlador.servicios.ServicioArchivo;
import controlador.servicios.ServicioCompra;
import controlador.servicios.ServicioCuotaCobrarPagar;
import controlador.servicios.ServicioDetalleVenta;
import controlador.servicios.ServicioLoteProducto;
import controlador.servicios.ServicioPersona;
import controlador.servicios.ServicioProveedor;

import controlador.servicios.ServicioServicios;
import controlador.servicios.ServicioVenta;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import vista.modeloTablas.ModeloTablaCuotas;
import vista.modeloTablas.ModeloTablaDeatalle;
import vista.modeloTablas.ModeloTablaLote;
import vista.modeloTablas.ModeloTablaProducto;
import vista.modeloTablas.ModeloTablaServicio;
import vista.utilidades.FacadeComponentes;

/**
 *
 * @author Alex
 */
public class PanelCompra extends javax.swing.JPanel {

    /**
     * Creates new form PanelVenta
     */
    private ServicioProveedor sp = new ServicioProveedor();
    private ServicioCompra sv = new ServicioCompra();
    private String dir="";
    private ServicioCuotaCobrarPagar scp = new ServicioCuotaCobrarPagar();
    //private ServicioLoteProducto spr = new ServicioLoteProducto();
    //private ServicioServicios ss = new ServicioServicios();
    private ModeloTablaCuotas modeloCuotas = new ModeloTablaCuotas();

    public PanelCompra() {
        initComponents();
        //     cargarTablaDetalle();
        //cargarTablaProducto();
        cargarTablaCuotas();
    }

    private void cargarTablaCuotas() {

        tbl_tablaCuotas.setModel(modeloCuotas);
        tbl_tablaCuotas.updateUI();
    }

    private void buscarPersona() {
        System.out.println(txtCedulaCliente.getText());
        sp.fijarInstancia(sp.obtenerPersonaCedula(txtCedulaCliente.getText()));
        if (sp.getProveedor().getId_persona() != null) {
            txtApellidosCliente.setText(sp.getProveedor().getApellidos_per());
            txtNombresCliente.setText(sp.getProveedor().getNombres_per());
            txtDirecciónCliente.setText(sp.getProveedor().getDireccion_per());
            txtTelfCellCliente.setText(sp.getProveedor().getFono_per());
        } else {
            JOptionPane.showMessageDialog(this, "No se ha encontrado ningun cliente con esta cédula", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCedulaCliente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNombresCliente = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtApellidosCliente = new javax.swing.JTextField();
        btnBuscarCliente = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtTelfCellCliente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDirecciónCliente = new javax.swing.JTextField();
        btnNuevoCliente = new javax.swing.JButton();
        txt_nrocompra = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtNumeroFactura = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtSubtotal = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtIva = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        btnNuevaFactura = new javax.swing.JButton();
        btnImprimirFactura = new javax.swing.JButton();
        btnCancelarB = new javax.swing.JButton();
        cbx_tipo = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txt_abono = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        cbx_nro = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbl_tablaCuotas = new javax.swing.JTable();
        jLabel16 = new javax.swing.JLabel();
        txt_descuento = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txt_factura = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        txt_date = new com.toedter.calendar.JDateChooser();

        setBorder(javax.swing.BorderFactory.createTitledBorder("SISTEMA DE FACTURACIÓN CELLTEC"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del Cliente"));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Nro. Cédula  /  RUC:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Ingrese los Nombres:");

        txtNombresCliente.setEnabled(false);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Ingrese los Apellidos:");

        txtApellidosCliente.setEnabled(false);
        txtApellidosCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtApellidosClienteActionPerformed(evt);
            }
        });

        btnBuscarCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/search.png"))); // NOI18N
        btnBuscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarClienteActionPerformed(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Teléfono Celular:");

        txtTelfCellCliente.setEnabled(false);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Dirección: ");

        txtDirecciónCliente.setEnabled(false);

        btnNuevoCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/add-item.png"))); // NOI18N

        txt_nrocompra.setEnabled(false);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Nro de compra:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCedulaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNuevoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombresCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelfCellCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txt_nrocompra)
                        .addGap(9, 9, 9))
                    .addComponent(txtApellidosCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                    .addComponent(txtDirecciónCliente))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtCedulaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNuevoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_nrocompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7)))
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombresCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(txtApellidosCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtDirecciónCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtTelfCellCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6))
                            .addComponent(jLabel5))
                        .addContainerGap())))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("FACTURA"));

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Fecha: ");

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("NRO. FACTURA: ");

        txtNumeroFactura.setFont(new java.awt.Font("Tahoma", 2, 18)); // NOI18N
        txtNumeroFactura.setForeground(new java.awt.Color(204, 0, 0));

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("SUBTOTAL:");

        txtSubtotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubtotalActionPerformed(evt);
            }
        });
        txtSubtotal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSubtotalKeyReleased(evt);
            }
        });

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("IVA:");

        txtIva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIvaActionPerformed(evt);
            }
        });
        txtIva.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIvaKeyReleased(evt);
            }
        });

        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("FACTURA:");

        txtTotal.setEnabled(false);

        btnNuevaFactura.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/add-item.png"))); // NOI18N
        btnNuevaFactura.setText("Nuevo");
        btnNuevaFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaFacturaActionPerformed(evt);
            }
        });

        btnImprimirFactura.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/print.png"))); // NOI18N
        btnImprimirFactura.setText("Imprimir");
        btnImprimirFactura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirFacturaActionPerformed(evt);
            }
        });

        btnCancelarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarB.setText("Cancelar");

        cbx_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Contado", "Credito" }));
        cbx_tipo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbx_tipoItemStateChanged(evt);
            }
        });

        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("TIPO DE PAGO");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("CUOTAS"));
        jPanel6.setLayout(null);

        jLabel4.setText("Abono:");
        jPanel6.add(jLabel4);
        jLabel4.setBounds(18, 35, 48, 16);

        txt_abono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_abonoKeyReleased(evt);
            }
        });
        jPanel6.add(txt_abono);
        txt_abono.setBounds(84, 30, 101, 26);

        jLabel15.setText("Nro Cuotas:");
        jPanel6.add(jLabel15);
        jLabel15.setBounds(197, 35, 81, 16);

        cbx_nro.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        cbx_nro.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbx_nroItemStateChanged(evt);
            }
        });
        jPanel6.add(cbx_nro);
        cbx_nro.setBounds(296, 30, 46, 26);

        tbl_tablaCuotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(tbl_tablaCuotas);

        jPanel6.add(jScrollPane3);
        jScrollPane3.setBounds(30, 70, 680, 90);

        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("DESCUENTO:");

        txt_descuento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_descuentoActionPerformed(evt);
            }
        });
        txt_descuento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_descuentoKeyReleased(evt);
            }
        });

        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setText("TOTAL:");

        jButton1.setText("Subir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel17, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(61, 61, 61)
                                        .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_factura, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbx_tipo, 0, 121, Short.MAX_VALUE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(61, 61, 61))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 745, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_descuento, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnNuevaFactura, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnImprimirFactura, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnCancelarB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(51, 51, 51))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(jLabel10)
                        .addComponent(txtNumeroFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cbx_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel14))
                    .addComponent(txt_date, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel12))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtIva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel16)
                        .addComponent(txt_descuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_factura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel17))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNuevaFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnImprimirFactura, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelarB, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtApellidosClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtApellidosClienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtApellidosClienteActionPerformed

    private void btnBuscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarClienteActionPerformed
        // TODO add your handling code here:
        buscarPersona();
    }//GEN-LAST:event_btnBuscarClienteActionPerformed

    private void btnNuevaFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaFacturaActionPerformed
        // TODO add your handling code here:
        limpiarCampos();
    }//GEN-LAST:event_btnNuevaFacturaActionPerformed

    private void btnImprimirFacturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirFacturaActionPerformed
        // TODO add your handling code here:
        //String aux = (modelo.getLista().isEmpty())?"":"hay";
        if (Utilidades.requerido(txtApellidosCliente, txtApellidosCliente.getText()) == true
                && Utilidades.requerido(txtTotal, txtTotal.getText()) == true) {
               // Utilidades.requerido(tablaFactura,aux)==true&&
            //Utilidades.requerido(txtTotal,txtTotal.getText())==true){
            cargarObjeto();
            if (this.sv.getCompra().getId_compra() == null) {

                if (this.sv.guardar() == true) {
                    this.subirArchivo();
                    sv.guardar();
                    JOptionPane.showMessageDialog(this, "Se ha registrado", "OK", JOptionPane.DEFAULT_OPTION, new ImageIcon(this.getClass().getResource("/vista/imagenes/confirmar.png")));
                    this.cargarTablaCuotas();
                    //this.irListar();
                } else {
                    JOptionPane.showMessageDialog(this, "No se ha registrado", "OK", JOptionPane.DEFAULT_OPTION, new ImageIcon(this.getClass().getResource("/vista/imagenes/no.png")));
                }
            } else {
                if (this.sv.getCompra().getCuentapc() != null) {
                    ServicioCuotaCobrarPagar axu = new ServicioCuotaCobrarPagar();
                    axu.fijarInstancia(sv.getCompra().getCuentapc());
                    if (scp.algunoPagado() == false) {
                        axu.eliminar();
                    }
                }

                if (this.sv.guardar() == true) {
                    this.subirArchivo();
                    JOptionPane.showMessageDialog(this, "Se ha modificado", "OK", JOptionPane.DEFAULT_OPTION, new ImageIcon(this.getClass().getResource("/vista/imagenes/confirmar.png")));
                    this.cargarTablaCuotas();
                    //this.irListar();
                } else {
                    JOptionPane.showMessageDialog(this, "No se ha modificado", "OK", JOptionPane.DEFAULT_OPTION, new ImageIcon(this.getClass().getResource("/vista/imagenes/no.png")));
                }
            }
        }
    }//GEN-LAST:event_btnImprimirFacturaActionPerformed

    private void cbx_tipoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbx_tipoItemStateChanged
        // TODO add your handling code here:
        if (cbx_tipo.getSelectedItem().toString().equalsIgnoreCase("contado")) {
            bloquear();
        } else {
            crearCuentasTablas();
        }

    }//GEN-LAST:event_cbx_tipoItemStateChanged

    private void txt_abonoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_abonoKeyReleased
        // TODO add your handling code here:
        crearCuentasTablas();
    }//GEN-LAST:event_txt_abonoKeyReleased

    private void cbx_nroItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbx_nroItemStateChanged
        // TODO add your handling code here://
        crearCuentasTablas();
    }//GEN-LAST:event_cbx_nroItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        subirArchivo();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtSubtotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubtotalActionPerformed
        // TODO add your handling code here:
        sumar();
        crearCuentasTablas();
    }//GEN-LAST:event_txtSubtotalActionPerformed

    private void txtIvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIvaActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_txtIvaActionPerformed

    private void txt_descuentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_descuentoActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_txt_descuentoActionPerformed

    private void txtSubtotalKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSubtotalKeyReleased
        // TODO add your handling code here:
        sumar();
        crearCuentasTablas();
    }//GEN-LAST:event_txtSubtotalKeyReleased

    private void txtIvaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIvaKeyReleased
        // TODO add your handling code here:
        sumar();
        crearCuentasTablas();
    }//GEN-LAST:event_txtIvaKeyReleased

    private void txt_descuentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_descuentoKeyReleased
        // TODO add your handling code here:
        sumar();
        crearCuentasTablas();
    }//GEN-LAST:event_txt_descuentoKeyReleased

    private void sumar() {
        double subtotal = 0;
        double iva = 0;
        double descuento = 0;
        double total = 0;
        try {
            subtotal = Double.parseDouble(txtSubtotal.getText());
            iva = Double.parseDouble(txtIva.getText());
            descuento = Double.parseDouble(txt_descuento.getText());
            
        } catch (Exception e) {
        }
        total = subtotal + iva - descuento;
        txtTotal.setText(String.valueOf(total));
    }

    private void cargarObjeto() {
        sv.getCompra().setProveedor(sp.getProveedor());
        sv.getCompra().setNumFact_compra(txtNumeroFactura.getText());
        sv.getCompra().setNum_compra(txt_nrocompra.getText());
        sv.getCompra().setFecha_compra(txt_date.getDate());
        sv.getCompra().setIva(Double.parseDouble(txtIva.getText()));
        sv.getCompra().setSubtotal(Double.parseDouble(txtSubtotal.getText()));
        sv.getCompra().setTotal(Double.parseDouble(txtSubtotal.getText()));
        sv.getCompra().setTipo_pago(cbx_tipo.getSelectedItem().toString());

    }

    private void limpiarCampos() {
        modeloCuotas.getLista().clear();
        txtSubtotal.setText("0");
        txtIva.setText("0");
        txtTotal.setText("0");

        sp.nuevaInstancia();

        sv.nuevaInstancia();
        txtApellidosCliente.setText("");
        txtCedulaCliente.setText("");
        //txtCriterio.setText("");
        txtDirecciónCliente.setText("");
        txt_date.setDate(new Date());
        //cbmCategoriaBusquedaProd.setSelectedIndex(0);
        txtNombresCliente.setText("");
        txtDirecciónCliente.setText("");
        cargarTablaCuotas();
        txt_factura.setText("");
        dir = "";
        cbx_nro.setSelectedIndex(0);
        cbx_tipo.setSelectedIndex(0);

    }

    private void cargarVista() {
        if (sv.getCompra().getArchivo() != null) {
            dir = sv.getCompra().getArchivo().getNombre_arch();
            txt_factura.setText(dir);
        }
    }

    private void subirArchivo() {
        if (dir.isEmpty() == false) {
            Utilidades.copiararchivos(dir, Utilidades.obtenerRutaRoot() + File.separatorChar + "compras" + File.separatorChar + this.sv.getCompra().getId_compra().intValue() + "-compra.pdf");
            ServicioArchivo sa = new ServicioArchivo();
            sa.fijarInstancia(sv.getCompra().getArchivo());
            sa.getArchivo().setDireccion_arch(Utilidades.obtenerRutaRoot() + File.separatorChar + "compras" + File.separatorChar);
            sa.getArchivo().setNombre_arch(this.sv.getCompra().getId_compra().intValue() + "-compra.pdf");
            sa.getArchivo().setCompra(sv.getCompra());
            sv.getCompra().setArchivo(sa.getArchivo());
        } else {
            JOptionPane.showMessageDialog(this, "No ha cargado el archivo", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void enviarDatosReporte(ServicioVenta sva) {
        HashMap parametros = new HashMap();
        parametros.put("nro_fact", sva.getVenta().getNro_venta());
        parametros.put("fecha", Utilidades.formatoFechaDateString(sva.getVenta().getFecha_venta()));
        parametros.put("cedula", sva.getVenta().getCliente().getCedula_per());
        parametros.put("cliente", sva.getVenta().getCliente().toString());
        parametros.put("direccion", sva.getVenta().getCliente().getDireccion_per());
        parametros.put("fono", sva.getVenta().getCliente().getFono_per());
        parametros.put("subtotal", sva.getVenta().getSubtotal());
        parametros.put("iva", sva.getVenta().getIva());
        parametros.put("descuento", 0.0);
        parametros.put("total", (sva.getVenta().getTotal()));
        new Reporte().crearReporte("factura", sva.getVenta().getListaDetalle(), "factura.pdf", parametros);
    }

    private void crearCuentasTablas() {
        modeloCuotas.getLista().clear();
        String tipo = cbx_tipo.getSelectedItem().toString();
        if (!tipo.equalsIgnoreCase("contado")) {
            double total = 0;
            try {
                total = Double.parseDouble(txtTotal.getText());
            } catch (Exception e) {
            }
            if (total > 0) {
                double abono = 0;
                try {
                    abono = Double.parseDouble(txt_abono.getText());
                } catch (Exception e) {
                }
                double saldo = total - abono;
                int nro = Integer.parseInt(cbx_nro.getSelectedItem().toString());
                ServicioCuotaCobrarPagar sccp = new ServicioCuotaCobrarPagar();
                sccp.getCuota().setAbono(abono);
                sccp.getCuota().setSaldo_actual(saldo);
                sccp.getCuota().setNro_cuotas(nro);
                sccp.getCuota().setTipo("venta");
                sccp.getCuota().setCompra(this.sv.getCompra());
                this.sv.getCompra().setCuentapc(sccp.getCuota());

                modeloCuotas.setLista(FacadeComponentes.crearCuotas(saldo, nro, sccp.getCuota(), tipo));
                tbl_tablaCuotas.updateUI();
            }
        } else {
            bloquear();
        }
    }

    private void bloquear() {
        txt_abono.setText("0");
        cbx_nro.setSelectedIndex(0);
        modeloCuotas.getLista().clear();
        tbl_tablaCuotas.updateUI();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscarCliente;
    private javax.swing.JButton btnCancelarB;
    private javax.swing.JButton btnImprimirFactura;
    private javax.swing.JButton btnNuevaFactura;
    private javax.swing.JButton btnNuevoCliente;
    private javax.swing.JComboBox cbx_nro;
    private javax.swing.JComboBox cbx_tipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tbl_tablaCuotas;
    private javax.swing.JTextField txtApellidosCliente;
    private javax.swing.JTextField txtCedulaCliente;
    private javax.swing.JTextField txtDirecciónCliente;
    private javax.swing.JTextField txtIva;
    private javax.swing.JTextField txtNombresCliente;
    private javax.swing.JTextField txtNumeroFactura;
    private javax.swing.JTextField txtSubtotal;
    private javax.swing.JTextField txtTelfCellCliente;
    private javax.swing.JTextField txtTotal;
    private javax.swing.JTextField txt_abono;
    private com.toedter.calendar.JDateChooser txt_date;
    private javax.swing.JTextField txt_descuento;
    private javax.swing.JTextField txt_factura;
    private javax.swing.JTextField txt_nrocompra;
    // End of variables declaration//GEN-END:variables
}
